package cn.zwk.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zwk.common.to.SkuHasStockTo;
import cn.zwk.common.utils.PageUtils;
import cn.zwk.common.utils.Query;
import cn.zwk.common.utils.R;
import cn.zwk.ware.dao.WareSkuDao;
import cn.zwk.ware.entity.WareSkuEntity;
import cn.zwk.ware.feign.ProductClient;
import cn.zwk.ware.service.WareSkuService;
import cn.zwk.ware.vo.SkuCountStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private ProductClient productClient;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         * skuId: 1
         * wareId: 2
         */
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if(!StringUtils.isEmpty(skuId)){
            queryWrapper.eq("sku_id",skuId);
        }

        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id",wareId);
        }


        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 添加库存
     * @param skuId
     * @param wareId
     * @param skuNum
     */
    @Transactional
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        LambdaQueryWrapper<WareSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WareSkuEntity::getWareId, wareId).
                eq(WareSkuEntity::getSkuId, skuId);
        WareSkuEntity entity = this.getOne(wrapper);
        if (entity==null){
            entity=new WareSkuEntity();
            entity.setSkuId(skuId);
            entity.setWareId(wareId);
            entity.setStock(skuNum);
            entity.setStockLocked(0);
            //todo 获取sku的名字
            try {
                R info = productClient.info(skuId);
                Map<String,Object> map = (Map<String, Object>) info.get("skuInfo");
                entity.setSkuName((String) map.get("skuName"));
            }catch (Exception e){
                log.error("远程调用异常",e);
            }
            this.save(entity);
        }else {
            entity.setStock(entity.getStock()+skuNum);
            this.updateById(entity);
        }
    }
    @Override
    public List<SkuHasStockTo> getSkuHasStock(List<Long> skuIds) {
        List<SkuCountStockVo> skuStockList = this.baseMapper.getSkuStock(skuIds);
        List<SkuHasStockTo> result = skuStockList.stream().map(
                s -> new SkuHasStockTo(s.getSkuId(), s.getStockCount() > 0)
        ).collect(Collectors.toList());
        return result;
    }

}
